package com.shh.shojbackendcommon.aop;

import cn.hutool.core.date.StopWatch;
import com.shh.shojbackendcommon.annotation.AuthCheck;
import com.shh.shojbackendcommon.annotation.LogExecutionTime;
import com.shh.shojbackendcommon.common.ErrorCode;
import com.shh.shojbackendcommon.exception.BusinessException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 *  用来记录程序执行的时间
 *  用处一:收集代码沙箱中代码的执行时间
 *  用处二:可以对方法执行的时间进行分析判断，进行性能调优
 */

@Aspect
@Component
public class TimeAop {


    /**
     * 执行拦截 @Around指的是在方法运行前后执行 @Before是指仅在方法前执行 @After指仅在方法后执行
     *
     * @param joinPoint
     * @param logExecutionTime
     * @return
     */
    @Around("@annotation(logExecutionTime)")
    public Object doInterceptor(ProceedingJoinPoint joinPoint, LogExecutionTime logExecutionTime) throws Throwable {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        Object obj = joinPoint.proceed();
        stopWatch.stop();
        long cost = stopWatch.getTotalTimeMillis();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        String methodName = signature.getDeclaringTypeName() + "." + signature.getName();
        System.out.println("methodName:" + methodName + " speedTime:" + cost);
        return obj;
    }

}
